package com.txsj.scan.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;


/**
 * Created by wwb on 2016/5/27.
 */
public class AppSetting {
    public static final String SP_NAME = "userinfo"; //用户基本信息表
    private static AppSetting instance;
    private static SharedPreferences mSp = null;


    //account  登录账号保存的品牌ID 或者  cbo手机号码
    //cboMobile  cbo 可以登录账号的手机号列表
    //bindMobile  品牌绑定的手机号
    //TagVerification 每次重新登录，（解绑标签、修改商品价格、添加\删除人脸）都需要验证
    //expireTime 萤石云，AccessToken过期时间
    //isFirstIn第一次进入APP  true  第一次进入
    //推送tag记录字段  pushTag

    private AppSetting() {
    }

    public static AppSetting getInstance() {
        if (instance == null)
            instance = new AppSetting();
        return instance;
    }

    public void init(Context context) {
        mSp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    /***
     * 公共的存储方法
     * @param key
     * @param value
     */
    public void setString(String key, String value) {
        SharedPreferences.Editor edit = mSp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public void setLong(String key, long value) {
        SharedPreferences.Editor edit = mSp.edit();
        edit.putLong(key, value);
        edit.commit();
    }

    public long getLong(String key) {
        return mSp.getLong(key, 0);
    }

    /**
     * 公共的获取方法
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return mSp.getString(key, "");
    }


    public void setBoolean(String key, boolean value) {
        SharedPreferences.Editor edit = mSp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public boolean getBoolean(String key) {
        return mSp.getBoolean(key, false);
    }

    /**
     * 获取当前登录状态
     *
     * @return
     */
    public boolean isLogin() {
        return mSp.getBoolean("isLogin", false) && !TextUtils.isEmpty(getUserId());
    }

    /***
     * 设置登录状态
     * @param flag
     */
    public void setIsLogin(boolean flag) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putBoolean("isLogin", flag);
        editor.commit();
    }


    /**
     * 保存userid
     *
     * @param userid
     */
    public void setUserId(String userid) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("userid", userid);
        editor.commit();
    }

    /**
     * 获取userId
     *
     * @return
     */
    public String getUserId() {
        return mSp.getString("userid", "");
    }

    /**
     * 保存brandid
     *
     * @param brandid
     */
    public void setBrandId(String brandid) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("BrandId", brandid);
        editor.commit();
    }

    /**
     * 获取BrandId
     *
     * @return
     */
    public String getBrandId() {
        return mSp.getString("BrandId", "");
    }

    /**
     * 保存token
     *
     * @param token
     */
    public void setToken(String token) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("token", token);
        editor.commit();
    }

    /**
     * 获取token
     *
     * @return
     */
    public String getToken() {
        return mSp.getString("token", "");
    }


    /**
     * 保存员工id
     *
     * @param employeeId
     */
    public void setEmployeeId(String employeeId) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("employeeid", employeeId);
        editor.commit();
    }

    /**
     * 获取员工id
     *
     * @return
     */
    public String getEmployeeid() {
        return mSp.getString("employeeid", "");
    }


    public void setEmployeeMd5Id(String md5Id) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("employeeMd5Id", md5Id);
        editor.commit();
    }

    public String getEmployeeMd5Id() {
        return mSp.getString("employeeMd5Id", "");
    }


    /**
     * 设置员工类型
     *
     * @param employeeType
     */
    public void setEmployeeType(int employeeType) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putInt("employeeType", employeeType);
        editor.commit();
    }

    /**
     * 设置员工类型
     *
     * @return
     */
    public int getEmployeeType() {
        return mSp.getInt("employeeType", 0);
    }


    /**
     * 获取推送标识
     *
     * @return
     */
    public String getPushTag() {
        return mSp.getString("pushTag", "");
    }


    /**
     * 保存推送标识
     *
     * @param pushTag
     */
    public void setPushTag(String pushTag) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("pushTag", pushTag);
        editor.commit();
    }


    /**
     * 设置供货商模块登录状态
     *
     * @param isZhuanLogin
     */
    public void setIsZhuanLogin(boolean isZhuanLogin) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putBoolean("isZhuanLogin", isZhuanLogin);
        editor.commit();
    }

    /**
     * 获取供货商模块登录状态
     *
     * @return
     */
    public boolean isZhuanLogin() {
        return mSp.getBoolean("isZhuanLogin", false) && !TextUtils.isEmpty(getZhuanUserId());
    }

    /**
     * 保存创业赚钱userid
     *
     * @param userid
     */
    public void setZhuanUserId(String userid) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("zhuanUserid", userid);
        editor.commit();
    }

    /**
     * 获取创业赚钱userid
     *
     * @return
     */
    public String getZhuanUserId() {
        return mSp.getString("zhuanUserid", "");
    }


    /**
     * 保存创业赚钱token
     *
     * @param userid
     */
    public void setZhuanToken(String userid) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("zhuanToken", userid);
        editor.commit();
    }

    /**
     * 获取创业赚钱token
     *
     * @return
     */
    public String getZhuanToken() {
        return mSp.getString("zhuanToken", "");
    }


    /**
     * 保存区域代码
     *
     * @param regionCode
     */
    public void setRegionCode(String regionCode) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putString("regionCode", regionCode);
        editor.commit();
    }

    /**
     * 获取选择的供货商区域
     *
     * @return
     */
    public String getRegionCode() {
        return mSp.getString("regionCode", "");
    }

    /**
     * 保存购物车商品数量
     *
     * @param count
     */
    public void setShopCartCount(int count) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.putInt("shopCount", count);
        editor.commit();
    }

    /**
     * 获取购物车商品数量
     *
     * @return
     */
    public int getShopCartCount() {
        return mSp.getInt("shopCount", 0);
    }
}
